{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.metrics import roc_auc_score, auc\n",
    "from sklearn.metrics import precision_recall_curve\n",
    "from sklearn.metrics import classification_report\n",
    "from collections import Counter\n",
    "import re\n",
    "from tqdm import trange"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pwd = '../../data/'\n",
    "peco_id_name = pd.read_excel(pwd + 'peco_name.xlsx')\n",
    "gene_id_name = pd.read_excel(pwd + 'gene_name.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def metrics(y_true, y_pred, y_prob):\n",
    "\n",
    "    tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()\n",
    "\n",
    "    pos_acc = tp / sum(y_true)\n",
    "    neg_acc = tn / (len(y_pred) - sum(y_pred)) # [y_true=0 & y_pred=0] / y_pred=0\n",
    "    accuracy = (tp+tn)/(tn+fp+fn+tp)\n",
    "    \n",
    "    recall = tp / (tp+fn)\n",
    "    precision = tp / (tp+fp)\n",
    "    f1 = 2*precision*recall / (precision+recall)\n",
    "    \n",
    "    roc_auc = roc_auc_score(y_true, y_prob)\n",
    "    prec, reca, _ = precision_recall_curve(y_true, y_prob)\n",
    "    aupr = auc(reca, prec)\n",
    "    average1 = (accuracy + precision + recall + roc_auc + aupr) / 5\n",
    "    average2 = (accuracy + f1 + roc_auc + aupr) / 4\n",
    "    average3 = (f1 + aupr) / 2\n",
    "    print('tn = {}, fp = {}, fn = {}, tp = {}'.format(tn, fp, fn, tp))\n",
    "    print('y_pred: 0 = {} | 1 = {}'.format(Counter(y_pred)[0], Counter(y_pred)[1]))\n",
    "    print('y_true: 0 = {} | 1 = {}'.format(Counter(y_true)[0], Counter(y_true)[1]))\n",
    "    print('acc={:.4f}|precision={:.4f}|recall={:.4f}|f1={:.4f}|auc={:.4f}|aupr={:.4f}|pos_acc={:.4f}|neg_acc={:.4f}'.format(accuracy, precision, recall, f1, roc_auc, aupr, pos_acc, neg_acc))\n",
    "    print('{:.4f}, {:.4f}, {:.4f}, {:.4f}, {:.4f}, {:.4f}, {:.4f}, {:.4f}, {:.4f}'.format(accuracy, precision, recall, f1, roc_auc, aupr, average1, average2, average3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_test_file():\n",
    "    train_test_id_idx = np.load('../../data/task_Tp__testlabel0_knn_edge_train_test_index_all.npz', allow_pickle = True)\n",
    "    train_index_all = train_test_id_idx['train_index_all']\n",
    "    test_index_all = train_test_id_idx['test_index_all']\n",
    "    train_id_all = train_test_id_idx['train_id_all'] # 'gene', 'peco'\n",
    "    test_id_all = train_test_id_idx['test_id_all'] # 'gene', 'peco'\n",
    "    return test_index_all, test_id_all, (train_index_all, train_id_all)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "def balanced_results_file(): #weight = None\n",
    "    file = np.load(\"ys.npz\")\n",
    "    y_true_train, y_pred_train, y_prob_train = file['arr_0'][0], file['arr_0'][1], file['arr_0'][2]\n",
    "    y_true_test, y_pred_test, y_prob_test = file['arr_1'][0], file['arr_1'][1], file['arr_1'][2] \n",
    "    \n",
    "    print('Train:')\n",
    "    metrics(y_true_train, y_pred_train, y_prob_train)\n",
    "    print('Test:')\n",
    "    metrics(y_true_test, y_pred_test, y_prob_test)\n",
    "    \n",
    "    return y_true_test, y_pred_test, y_prob_test, (y_true_train, y_pred_train, y_prob_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5-methyltryptophan exposure\n",
      "tn = 204, fp = 21, fn = 2, tp = 23\n",
      "y_pred: 0 = 206 | 1 = 44\n",
      "y_true: 0 = 225 | 1 = 25\n",
      "acc=0.9080|precision=0.5227|recall=0.9200|f1=0.6667|auc=0.9737|aupr=0.8737|pos_acc=0.9200|neg_acc=0.9903\n",
      "0.9080, 0.5227, 0.9200, 0.6667, 0.9737, 0.8737, 0.8396, 0.8555, 0.7702\n",
      "*******************\n",
      "Magnaporthe grisea exposure\n",
      "tn = 106, fp = 10, fn = 72, tp = 911\n",
      "y_pred: 0 = 178 | 1 = 921\n",
      "y_true: 0 = 116 | 1 = 983\n",
      "acc=0.9254|precision=0.9891|recall=0.9268|f1=0.9569|auc=0.9740|aupr=0.9969|pos_acc=0.9268|neg_acc=0.5955\n",
      "0.9254, 0.9891, 0.9268, 0.9569, 0.9740, 0.9969, 0.9624, 0.9633, 0.9769\n",
      "*******************\n",
      "Nilaparvata lugens exposure\n",
      "tn = 182, fp = 29, fn = 4, tp = 12\n",
      "y_pred: 0 = 186 | 1 = 41\n",
      "y_true: 0 = 211 | 1 = 16\n",
      "acc=0.8546|precision=0.2927|recall=0.7500|f1=0.4211|auc=0.9162|aupr=0.4719|pos_acc=0.7500|neg_acc=0.9785\n",
      "0.8546, 0.2927, 0.7500, 0.4211, 0.9162, 0.4719, 0.6571, 0.6659, 0.4465\n",
      "*******************\n",
      "Pseudomonas avenae exposure\n",
      "tn = 174, fp = 15, fn = 2, tp = 11\n",
      "y_pred: 0 = 176 | 1 = 26\n",
      "y_true: 0 = 189 | 1 = 13\n",
      "acc=0.9158|precision=0.4231|recall=0.8462|f1=0.5641|auc=0.9597|aupr=0.6668|pos_acc=0.8462|neg_acc=0.9886\n",
      "0.9158, 0.4231, 0.8462, 0.5641, 0.9597, 0.6668, 0.7623, 0.7766, 0.6155\n",
      "*******************\n",
      "Xanthomonas oryzae pv. oryzae exposure\n",
      "tn = 175, fp = 32, fn = 8, tp = 26\n",
      "y_pred: 0 = 183 | 1 = 58\n",
      "y_true: 0 = 207 | 1 = 34\n",
      "acc=0.8340|precision=0.4483|recall=0.7647|f1=0.5652|auc=0.8760|aupr=0.6048|pos_acc=0.7647|neg_acc=0.9563\n",
      "0.8340, 0.4483, 0.7647, 0.5652, 0.8760, 0.6048, 0.7055, 0.7200, 0.5850\n",
      "*******************\n",
      "abscisic acid exposure\n",
      "tn = 155, fp = 19, fn = 29, tp = 191\n",
      "y_pred: 0 = 184 | 1 = 210\n",
      "y_true: 0 = 174 | 1 = 220\n",
      "acc=0.8782|precision=0.9095|recall=0.8682|f1=0.8884|auc=0.9561|aupr=0.9705|pos_acc=0.8682|neg_acc=0.8424\n",
      "0.8782, 0.9095, 0.8682, 0.8884, 0.9561, 0.9705, 0.9165, 0.9233, 0.9295\n",
      "*******************\n",
      "aluminum nutrient exposure\n",
      "tn = 183, fp = 22, fn = 0, tp = 3\n",
      "y_pred: 0 = 183 | 1 = 25\n",
      "y_true: 0 = 205 | 1 = 3\n",
      "acc=0.8942|precision=0.1200|recall=1.0000|f1=0.2143|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "0.8942, 0.1200, 1.0000, 0.2143, 1.0000, 1.0000, 0.8028, 0.7771, 0.6071\n",
      "*******************\n",
      "benzothiadiazole exposure\n",
      "tn = 194, fp = 21, fn = 3, tp = 8\n",
      "y_pred: 0 = 197 | 1 = 29\n",
      "y_true: 0 = 215 | 1 = 11\n",
      "acc=0.8938|precision=0.2759|recall=0.7273|f1=0.4000|auc=0.9362|aupr=0.5009|pos_acc=0.7273|neg_acc=0.9848\n",
      "0.8938, 0.2759, 0.7273, 0.4000, 0.9362, 0.5009, 0.6668, 0.6827, 0.4505\n",
      "*******************\n",
      "benzyladenine exposure\n",
      "tn = 195, fp = 22, fn = 0, tp = 1\n",
      "y_pred: 0 = 195 | 1 = 23\n",
      "y_true: 0 = 217 | 1 = 1\n",
      "acc=0.8991|precision=0.0435|recall=1.0000|f1=0.0833|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "0.8991, 0.0435, 1.0000, 0.0833, 1.0000, 1.0000, 0.7885, 0.7456, 0.5417\n",
      "*******************\n",
      "brassinosteroid exposure\n",
      "tn = 189, fp = 20, fn = 0, tp = 5\n",
      "y_pred: 0 = 189 | 1 = 25\n",
      "y_true: 0 = 209 | 1 = 5\n",
      "acc=0.9065|precision=0.2000|recall=1.0000|f1=0.3333|auc=1.0000|aupr=1.0000|pos_acc=1.0000|neg_acc=1.0000\n",
      "0.9065, 0.2000, 1.0000, 0.3333, 1.0000, 1.0000, 0.8213, 0.8100, 0.6667\n",
      "*******************\n",
      "**\n",
      "continuous dark (no light) exposure\n",
      "tn = 166, fp = 24, fn = 36, tp = 133\n",
      "y_pred: 0 = 202 | 1 = 157\n",
      "y_true: 0 = 190 | 1 = 169\n",
      "acc=0.8329|precision=0.8471|recall=0.7870|f1=0.8160|auc=0.9131|aupr=0.9015|pos_acc=0.7870|neg_acc=0.8218\n",
      "0.8329, 0.8471, 0.7870, 0.8160, 0.9131, 0.9015, 0.8563, 0.8659, 0.8587\n",
      "*******************\n",
      "field study\n",
      "tn = 194, fp = 35, fn = 1, tp = 9\n",
      "y_pred: 0 = 195 | 1 = 44\n",
      "y_true: 0 = 229 | 1 = 10\n",
      "acc=0.8494|precision=0.2045|recall=0.9000|f1=0.3333|auc=0.9734|aupr=0.8450|pos_acc=0.9000|neg_acc=0.9949\n",
      "0.8494, 0.2045, 0.9000, 0.3333, 0.9734, 0.8450, 0.7545, 0.7503, 0.5892\n",
      "*******************\n",
      "flood water exposure\n",
      "tn = 185, fp = 22, fn = 0, tp = 1\n",
      "y_pred: 0 = 185 | 1 = 23\n",
      "y_true: 0 = 207 | 1 = 1\n",
      "acc=0.8942|precision=0.0435|recall=1.0000|f1=0.0833|auc=0.9565|aupr=0.0500|pos_acc=1.0000|neg_acc=1.0000\n",
      "0.8942, 0.0435, 1.0000, 0.0833, 0.9565, 0.0500, 0.5888, 0.4960, 0.0667\n",
      "*******************\n",
      "**\n",
      "greenhouse study\n",
      "tn = 194, fp = 30, fn = 2, tp = 21\n",
      "y_pred: 0 = 196 | 1 = 51\n",
      "y_true: 0 = 224 | 1 = 23\n",
      "acc=0.8704|precision=0.4118|recall=0.9130|f1=0.5676|auc=0.9590|aupr=0.8663|pos_acc=0.9130|neg_acc=0.9898\n",
      "0.8704, 0.4118, 0.9130, 0.5676, 0.9590, 0.8663, 0.8041, 0.8158, 0.7169\n",
      "*******************\n",
      "laboratory study\n",
      "tn = 202, fp = 17, fn = 1, tp = 0\n",
      "y_pred: 0 = 203 | 1 = 17\n",
      "y_true: 0 = 219 | 1 = 1\n",
      "acc=0.9182|precision=0.0000|recall=0.0000|f1=nan|auc=0.6119|aupr=0.0058|pos_acc=0.0000|neg_acc=0.9951\n",
      "0.9182, 0.0000, 0.0000, nan, 0.6119, 0.0058, 0.3072, nan, nan\n",
      "*******************\n",
      "long day length exposure\n",
      "tn = 169, fp = 29, fn = 15, tp = 153\n",
      "y_pred: 0 = 184 | 1 = 182\n",
      "y_true: 0 = 198 | 1 = 168\n",
      "acc=0.8798|precision=0.8407|recall=0.9107|f1=0.8743|auc=0.9413|aupr=0.9312|pos_acc=0.9107|neg_acc=0.9185\n",
      "0.8798, 0.8407, 0.9107, 0.8743, 0.9413, 0.9312, 0.9007, 0.9066, 0.9027\n",
      "*******************\n",
      "methyl jasmonate exposure\n",
      "tn = 181, fp = 26, fn = 0, tp = 15\n",
      "y_pred: 0 = 181 | 1 = 41\n",
      "y_true: 0 = 207 | 1 = 15\n",
      "acc=0.8829|precision=0.3659|recall=1.0000|f1=0.5357|auc=0.9800|aupr=0.7847|pos_acc=1.0000|neg_acc=1.0000\n",
      "0.8829, 0.3659, 1.0000, 0.5357, 0.9800, 0.7847, 0.8027, 0.7958, 0.6602\n",
      "*******************\n",
      "rice yellow mottle virus exposure\n",
      "tn = 188, fp = 26, fn = 5, tp = 54\n",
      "y_pred: 0 = 193 | 1 = 80\n",
      "y_true: 0 = 214 | 1 = 59\n",
      "acc=0.8864|precision=0.6750|recall=0.9153|f1=0.7770|auc=0.9556|aupr=0.8782|pos_acc=0.9153|neg_acc=0.9741\n",
      "0.8864, 0.6750, 0.9153, 0.7770, 0.9556, 0.8782, 0.8621, 0.8743, 0.8276\n",
      "*******************\n",
      "short day length exposure\n",
      "tn = 174, fp = 24, fn = 19, tp = 88\n",
      "y_pred: 0 = 193 | 1 = 112\n",
      "y_true: 0 = 198 | 1 = 107\n",
      "acc=0.8590|precision=0.7857|recall=0.8224|f1=0.8037|auc=0.9000|aupr=0.8399|pos_acc=0.8224|neg_acc=0.9016\n",
      "0.8590, 0.7857, 0.8224, 0.8037, 0.9000, 0.8399, 0.8414, 0.8506, 0.8218\n",
      "*******************\n",
      "sodium chloride exposure\n",
      "tn = 149, fp = 23, fn = 30, tp = 307\n",
      "y_pred: 0 = 179 | 1 = 330\n",
      "y_true: 0 = 172 | 1 = 337\n",
      "acc=0.8959|precision=0.9303|recall=0.9110|f1=0.9205|auc=0.9560|aupr=0.9785|pos_acc=0.9110|neg_acc=0.8324\n",
      "0.8959, 0.9303, 0.9110, 0.9205, 0.9560, 0.9785, 0.9343, 0.9377, 0.9495\n",
      "*******************\n",
      "unknown exposure\n",
      "tn = 14, fp = 3, fn = 322, tp = 1800\n",
      "y_pred: 0 = 336 | 1 = 1803\n",
      "y_true: 0 = 17 | 1 = 2122\n",
      "acc=0.8481|precision=0.9983|recall=0.8483|f1=0.9172|auc=0.9076|aupr=0.9991|pos_acc=0.8483|neg_acc=0.0417\n",
      "0.8481, 0.9983, 0.8483, 0.9172, 0.9076, 0.9991, 0.9203, 0.9180, 0.9582\n",
      "*******************\n",
      "watering exposure\n",
      "tn = 172, fp = 16, fn = 94, tp = 341\n",
      "y_pred: 0 = 266 | 1 = 357\n",
      "y_true: 0 = 188 | 1 = 435\n",
      "acc=0.8234|precision=0.9552|recall=0.7839|f1=0.8611|auc=0.9220|aupr=0.9667|pos_acc=0.7839|neg_acc=0.6466\n",
      "0.8234, 0.9552, 0.7839, 0.8611, 0.9220, 0.9667, 0.8902, 0.8933, 0.9139\n",
      "*******************\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-6-41e23ccebf3e>:11: RuntimeWarning: invalid value encountered in double_scalars\n",
      "  f1 = 2*precision*recall / (precision+recall)\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv('Tp_balanced_case_study_0.csv').groupby('trait')\n",
    "for name,group in df:\n",
    "    a = pd.DataFrame(group)\n",
    "    if a['y_true'].mean()==0 or a['y_true'].mean()==1:        \n",
    "        print('**')\n",
    "    else:\n",
    "        print(name)\n",
    "        metrics(a['y_true'], a['y_pred'], a['y_prob'])        \n",
    "        print('*******************')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sample(random_seed):\n",
    "    all_associations = pd.read_csv('D:/小麦/MDA-GCNFTG-main/MDA-GCNFTG-main/data/all_gpe_pairs.csv', names=['gene', 'disease', 'label'])\n",
    "    known_associations = all_associations.loc[all_associations['label'] == 1]\n",
    "    unknown_associations = all_associations.loc[all_associations['label'] == 0]\n",
    "    random_negative = unknown_associations.sample(n=known_associations.shape[0], random_state=random_seed, axis=0)\n",
    "\n",
    "    sample_df = known_associations.append(random_negative)\n",
    "    sample_df.reset_index(drop=True, inplace=True)\n",
    "\n",
    "    return sample_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_balanced_Tp(task, balance, knn, lr):\n",
    "    test_index_all, test_id_all, _ = train_test_file()# '__nobalance'\n",
    "\n",
    "    for i in range(1):\n",
    "        print('==== Fold ', i)\n",
    "        y_true_test, y_pred_test, y_prob_test, _ = balanced_results_file()\n",
    "\n",
    "        if i == 0:\n",
    "             y_true_test_all, y_pred_test_all, y_prob_test_all = y_true_test, y_pred_test, y_prob_test\n",
    "            \n",
    "        else:\n",
    "            y_true_test_all = np.vstack([y_true_test_all, y_true_test])\n",
    "            y_pred_test_all = np.vstack([y_pred_test_all, y_pred_test])\n",
    "            y_prob_test_all = np.vstack([y_prob_test_all, y_prob_test])\n",
    "            assert (y_prob_test_all[i] == y_prob_test).all()\n",
    "\n",
    "    results_df = pd.DataFrame(test_id_all[0].reshape(-1, 2), columns = ['gene', 'peco'])\n",
    "    print(len(results_df))\n",
    "    print(len(y_true_test_all.reshape(-1)))\n",
    "    results_df['y_true'] = y_true_test_all.reshape(-1)\n",
    "    results_df['y_pred'] = y_pred_test_all.reshape(-1)\n",
    "    results_df['y_prob'] = y_prob_test_all.reshape(-1)\n",
    "\n",
    "    print(results_df)\n",
    "    print(gene_id_name)\n",
    "    results_df = pd.merge(results_df, gene_id_name, left_on = 'gene', right_index = True)\n",
    "    results_df = pd.merge(results_df, peco_id_name, left_on = 'peco', right_index = True)\n",
    "    #results_df.drop(labels = ['id_x', 'id_y'], axis = 1, inplace = True)\n",
    "    results_df.sort_values(by = ['peco', 'y_prob'], ascending = False, inplace = True)\n",
    "    \n",
    "    results_df.to_csv(task + '_balanced_case_study_0.csv')\n",
    "    \n",
    "    return results_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_balanced_Tgp(task, balance, knn, lr):\n",
    "    dtp = sample(random_seed = 1234)\n",
    "    test_index_all, test_id_all, _ = train_test_file(task, balance)# '__nobalance'\n",
    "\n",
    "    for i in range(5):\n",
    "        print('==== Fold ', i)\n",
    "        y_true_test, y_pred_test, y_prob_test, _ = balanced_results_file(task, knn, lr, fold = i)\n",
    "\n",
    "        temp = dtp.iloc[test_index_all[i]][['gene', 'peco']]\n",
    "        if i == 0:\n",
    "            y_true_test_all, y_pred_test_all, y_prob_test_all = y_true_test, y_pred_test, y_prob_test\n",
    "            \n",
    "            results_df = temp\n",
    "        else:\n",
    "            y_true_test_all = np.hstack([y_true_test_all, y_true_test])\n",
    "            y_pred_test_all = np.hstack([y_pred_test_all, y_pred_test])\n",
    "            y_prob_test_all = np.hstack([y_prob_test_all, y_prob_test])\n",
    "            \n",
    "            results_df = pd.concat([results_df, temp], axis = 0)\n",
    "            \n",
    "    results_df['y_true'] = y_true_test_all.reshape(-1)\n",
    "    results_df['y_pred'] = y_pred_test_all.reshape(-1)\n",
    "    results_df['y_prob'] = y_prob_test_all.reshape(-1)\n",
    "\n",
    "    results_df = pd.merge(results_df, gene_id_name, left_on = 'gene', right_on = 'id')\n",
    "    results_df = pd.merge(results_df, peco_id_name, left_on = 'peco', right_on = 'id')\n",
    "    results_df.drop(labels = ['id_x', 'id_y'], axis = 1, inplace = True)\n",
    "    results_df.sort_values(by = ['peco_x', 'y_prob'], ascending = False, inplace = True)\n",
    "    \n",
    "    results_df.to_csv(task + '_balanced_case_study_0.csv')\n",
    "    \n",
    "    return results_df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run balanced"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==== Fold  0\n",
      "Train:\n",
      "tn = 1336, fp = 113, fn = 172, tp = 1250\n",
      "y_pred: 0 = 1508 | 1 = 1363\n",
      "y_true: 0 = 1449 | 1 = 1422\n",
      "acc=0.9007|precision=0.9171|recall=0.8790|f1=0.8977|auc=0.9685|aupr=0.9699|pos_acc=0.8790|neg_acc=0.8859\n",
      "0.9007, 0.9171, 0.8790, 0.8977, 0.9685, 0.9699, 0.9270, 0.9342, 0.9338\n",
      "Test:\n",
      "tn = 4133, fp = 533, fn = 645, tp = 4113\n",
      "y_pred: 0 = 4778 | 1 = 4646\n",
      "y_true: 0 = 4666 | 1 = 4758\n",
      "acc=0.8750|precision=0.8853|recall=0.8644|f1=0.8747|auc=0.9428|aupr=0.9492|pos_acc=0.8644|neg_acc=0.8650\n",
      "0.8750, 0.8853, 0.8644, 0.8747, 0.9428, 0.9492, 0.9033, 0.9104, 0.9120\n",
      "9424\n",
      "9424\n",
      "       gene  peco  y_true  y_pred    y_prob\n",
      "0        33     0     1.0     1.0  0.997558\n",
      "1        94     0     1.0     1.0  0.998668\n",
      "2        97     0     1.0     1.0  0.998778\n",
      "3       155     0     1.0     1.0  0.999781\n",
      "4       214     0     1.0     1.0  1.000000\n",
      "...     ...   ...     ...     ...       ...\n",
      "9419   4839     3     0.0     1.0  0.508719\n",
      "9420   5250    19     0.0     0.0  0.109928\n",
      "9421   7394    14     0.0     0.0  0.329695\n",
      "9422  10270    24     0.0     0.0  0.408970\n",
      "9423   3432     9     0.0     1.0  0.527762\n",
      "\n",
      "[9424 rows x 5 columns]\n",
      "                                           gene\n",
      "0                                LOC_Os01g64660\n",
      "1                                LOC_Os03g38000\n",
      "2                                LOC_Os10g20630\n",
      "3      BTH-induced ERF transcriptional factor 2\n",
      "4                        Ent-kaurene synthase 6\n",
      "...                                         ...\n",
      "12182                            LOC_Os10g09990\n",
      "12183                            LOC_Os02g38780\n",
      "12184                            LOC_Os07g31130\n",
      "12185                            LOC_Os11g29710\n",
      "12186                            LOC_Os06g46920\n",
      "\n",
      "[12187 rows x 1 columns]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>gene</th>\n",
       "      <th>gene_x</th>\n",
       "      <th>peco</th>\n",
       "      <th>y_true</th>\n",
       "      <th>y_pred</th>\n",
       "      <th>y_prob</th>\n",
       "      <th>gene_y</th>\n",
       "      <th>trait</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7471</th>\n",
       "      <td>2020</td>\n",
       "      <td>2020</td>\n",
       "      <td>31</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.986779</td>\n",
       "      <td>LOC_Os06g20410</td>\n",
       "      <td>laboratory study</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6785</th>\n",
       "      <td>5236</td>\n",
       "      <td>5236</td>\n",
       "      <td>31</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.961113</td>\n",
       "      <td>LOC_Os04g12890</td>\n",
       "      <td>laboratory study</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5374</th>\n",
       "      <td>9991</td>\n",
       "      <td>9991</td>\n",
       "      <td>31</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.945579</td>\n",
       "      <td>LOC_Os05g46720</td>\n",
       "      <td>laboratory study</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6716</th>\n",
       "      <td>8717</td>\n",
       "      <td>8717</td>\n",
       "      <td>31</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.943131</td>\n",
       "      <td>LOC_Os01g29409</td>\n",
       "      <td>laboratory study</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6939</th>\n",
       "      <td>4814</td>\n",
       "      <td>4814</td>\n",
       "      <td>31</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.931262</td>\n",
       "      <td>LOC_Os06g22550</td>\n",
       "      <td>laboratory study</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8885</th>\n",
       "      <td>2330</td>\n",
       "      <td>2330</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000093</td>\n",
       "      <td>LOC_Os11g38860</td>\n",
       "      <td>sodium chloride exposure</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7238</th>\n",
       "      <td>6070</td>\n",
       "      <td>6070</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000074</td>\n",
       "      <td>LOC_Os03g24860</td>\n",
       "      <td>sodium chloride exposure</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8010</th>\n",
       "      <td>6463</td>\n",
       "      <td>6463</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000067</td>\n",
       "      <td>LOC_Os02g32760</td>\n",
       "      <td>sodium chloride exposure</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7237</th>\n",
       "      <td>8836</td>\n",
       "      <td>8836</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000035</td>\n",
       "      <td>LOC_Os04g58840</td>\n",
       "      <td>sodium chloride exposure</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4856</th>\n",
       "      <td>503</td>\n",
       "      <td>503</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000015</td>\n",
       "      <td>LOC_Os08g41500</td>\n",
       "      <td>sodium chloride exposure</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9424 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      gene  gene_x  peco  y_true  y_pred    y_prob          gene_y  \\\n",
       "7471  2020    2020    31     0.0     1.0  0.986779  LOC_Os06g20410   \n",
       "6785  5236    5236    31     0.0     1.0  0.961113  LOC_Os04g12890   \n",
       "5374  9991    9991    31     0.0     1.0  0.945579  LOC_Os05g46720   \n",
       "6716  8717    8717    31     0.0     1.0  0.943131  LOC_Os01g29409   \n",
       "6939  4814    4814    31     0.0     1.0  0.931262  LOC_Os06g22550   \n",
       "...    ...     ...   ...     ...     ...       ...             ...   \n",
       "8885  2330    2330     0     0.0     0.0  0.000093  LOC_Os11g38860   \n",
       "7238  6070    6070     0     0.0     0.0  0.000074  LOC_Os03g24860   \n",
       "8010  6463    6463     0     0.0     0.0  0.000067  LOC_Os02g32760   \n",
       "7237  8836    8836     0     0.0     0.0  0.000035  LOC_Os04g58840   \n",
       "4856   503     503     0     0.0     0.0  0.000015  LOC_Os08g41500   \n",
       "\n",
       "                         trait  \n",
       "7471          laboratory study  \n",
       "6785          laboratory study  \n",
       "5374          laboratory study  \n",
       "6716          laboratory study  \n",
       "6939          laboratory study  \n",
       "...                        ...  \n",
       "8885  sodium chloride exposure  \n",
       "7238  sodium chloride exposure  \n",
       "8010  sodium chloride exposure  \n",
       "7237  sodium chloride exposure  \n",
       "4856  sodium chloride exposure  \n",
       "\n",
       "[9424 rows x 8 columns]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results_Tp_balanced = run_balanced_Tp(task = 'Tp', balance = '', knn = '10knn', lr = 0.001)\n",
    "results_Tp_balanced"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
